package com.tianyi.lib.result;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class GenerateBaseMenuResult{

    public static List<BaseMenuResult> generateByKeys(List<Long> keys){

        List<String> keyStrs = new ArrayList<>();
        for (Long key : keys){
            keyStrs.add(key+"");
        }
        Collections.sort(keyStrs);
        List<BaseMenuResult> menuResults = new ArrayList<>();
        boolean prevSameBaseflag = false;

        for(String keyStr : keyStrs) {
            List<BaseMenuResult> baseMenuResults = new ArrayList<>();
            switch (keyStr.charAt(0)) {
                case '1':
                    menuResults.add(generateByAttributes("1", "pie-chart",
                            "概览", "/tianyi/overview", null));
                    break;

                case '2':
                    if(keyStr.length()==1){
                        baseMenuResults.add(generateByAttributes("21", null, "租借用户信息库",
                                "/tianyi/rentUserList", null));
                        baseMenuResults.add(generateByAttributes("22", null, "后台用户信息库",
                                "/tianyi/adminUserList", null));
                        baseMenuResults.add(generateByAttributes("23", null, "权限架构设置",
                                "/tianyi/authFramework", null));
                        menuResults.add(generateByAttributes("2", "contacts",
                                "用户管理", null, baseMenuResults));
                    }else if(keyStr.length()==2) {
                        if((menuResults.size()>0)&&(menuResults.get(menuResults.size()-1).
                                getKey().charAt(0)=='2')){
                            prevSameBaseflag = true;
                        }
                        switch (keyStr.charAt(1)){
                            case '1':
                                if(!prevSameBaseflag) {
                                    baseMenuResults.add(generateByAttributes("21", null, "租借用户信息库",
                                            "/tianyi/rentUserList", null));
                                    menuResults.add(generateByAttributes("2", "contacts",
                                            "用户管理", null, baseMenuResults));
                                }else{
                                    menuResults.get(menuResults.size()-1).getChildren().add(generateByAttributes("21", null, "租借用户信息库",
                                            "/tianyi/rentUserList", null));
                                }
                                break;
                            case '2':
                                if(!prevSameBaseflag) {
                                    baseMenuResults.add(generateByAttributes("21", null, "租借用户信息库",
                                            "/tianyi/rentUserList", null));
                                    menuResults.add(generateByAttributes("2", "contacts",
                                            "用户管理", null, baseMenuResults));
                                }else{
                                    menuResults.get(menuResults.size()-1).getChildren().add(generateByAttributes("22", null,
                                            "后台用户信息库", "/tianyi/adminUserList", null));
                                }
                                break;
                            case '3':
                                if(!prevSameBaseflag) {
                                    baseMenuResults.add(generateByAttributes("21", null, "租借用户信息库",
                                            "/tianyi/rentUserList", null));
                                    menuResults.add(generateByAttributes("2", "contacts",
                                            "用户管理", null, baseMenuResults));
                                }else{
                                    menuResults.get(menuResults.size()-1).getChildren().add(generateByAttributes("23", null,
                                            "权限架构设置", "/tianyi/authFramework", null));
                                }
                                break;
                        }
                    }
                    prevSameBaseflag = false;
                    break;

                case '3':
                    prevSameBaseflag = false;
                    break;

                case '4':
                    if(keyStr.length()==1) {
                        baseMenuResults.add(generateByAttributes("41", null, "画册管理",
                                "/tianyi/catalogList", null));
                        baseMenuResults.add(generateByAttributes("42", null, "产品管理",
                                "/tianyi/productList", null));
                        baseMenuResults.add(generateByAttributes("43", null, "画册上传",
                                "/tianyi/catalogUpload", null));
                        baseMenuResults.add(generateByAttributes("44", null, "产品上传",
                                "/tianyi/productUpload", null));
                        menuResults.add(generateByAttributes("4", "container",
                                "品牌与产品管理", null, baseMenuResults));
                    }else if(keyStr.length()==2){
                        if((menuResults.size()>0)&&(menuResults.get(menuResults.size()-1).
                                getKey().charAt(0)=='4')){
                            prevSameBaseflag = true;
                        }
                        switch (keyStr.charAt(1)) {
                            case '1':
                                if(!prevSameBaseflag) {
                                    baseMenuResults.add(generateByAttributes("41", null, "画册管理",
                                            "/tianyi/catalogList", null));
                                    menuResults.add(generateByAttributes("4", "container",
                                            "品牌与产品管理", null, baseMenuResults));
                                }else{
                                    menuResults.get(menuResults.size()-1).getChildren().add(generateByAttributes("41", null, "画册管理",
                                            "/tianyi/catalogList", null));
                                }
                                break;
                            case '2':
                                if(!prevSameBaseflag) {
                                    baseMenuResults.add(generateByAttributes("42", null, "产品管理",
                                            "/tianyi/productList", null));
                                    menuResults.add(generateByAttributes("4", "container",
                                            "品牌与产品管理", null, baseMenuResults));
                                }else{
                                    menuResults.get(menuResults.size()-1).getChildren().add(generateByAttributes("42", null, "产品管理",
                                            "/tianyi/productList", null));
                                }
                                break;
                            case '3':
                                if(!prevSameBaseflag) {
                                    baseMenuResults.add(generateByAttributes("43", null, "画册上传",
                                            "/tianyi/catalogUpload", null));
                                    menuResults.add(generateByAttributes("4", "container",
                                            "品牌与产品管理", null, baseMenuResults));
                                }else{
                                    menuResults.get(menuResults.size()-1).getChildren().add(generateByAttributes("43", null, "画册上传",
                                            "/tianyi/catalogUpload", null));
                                }
                                break;
                            case '4':
                                if(!prevSameBaseflag) {
                                    baseMenuResults.add(generateByAttributes("44", null, "产品上传",
                                            "/tianyi/productUpload", null));
                                    menuResults.add(generateByAttributes("4", "container",
                                            "品牌与产品管理", null, baseMenuResults));
                                }else{
                                    menuResults.get(menuResults.size()-1).getChildren().add(generateByAttributes("44", null, "产品上传",
                                            "/tianyi/productUpload", null));
                                }
                                break;
                        }
                    }
                    prevSameBaseflag = false;
                    break;

                case '5':
                    if(keyStr.length()==1) {
                        baseMenuResults.add(generateByAttributes("51", null, "待审核订单",
                                "/tianyi/applyingOrders", null));
                        baseMenuResults.add(generateByAttributes("52", null, "待发货订单",
                                "/tianyi/sentOrders", null));
                        baseMenuResults.add(generateByAttributes("53", null, "待催还订单",
                                "/tianyi/remindingOrders", null));
                        baseMenuResults.add(generateByAttributes("54", null, "已完成订单",
                                "/tianyi/completedOrders", null));
                        menuResults.add(generateByAttributes("5", "calendar",
                                "预约租借管理", null, baseMenuResults));
                    }else if(keyStr.length()==2){
                        if((menuResults.size()>0)&&(menuResults.get(menuResults.size()-1).
                                getKey().charAt(0)=='5')){
                            prevSameBaseflag = true;
                        }
                        switch (keyStr.charAt(1)) {
                            case '1':
                                if(!prevSameBaseflag) {
                                    baseMenuResults.add(generateByAttributes("51", null, "待审核订单",
                                            "/tianyi/applyingOrders", null));
                                    menuResults.add(generateByAttributes("5", "calendar",
                                            "预约租借管理", null, baseMenuResults));
                                }else{
                                    menuResults.get(menuResults.size()-1).getChildren().add(generateByAttributes("51", null, "待审核订单",
                                            "/tianyi/applyingOrders", null));
                                }
                                break;
                            case '2':
                                if(!prevSameBaseflag) {
                                    baseMenuResults.add(generateByAttributes("52", null, "待发货订单",
                                            "/tianyi/sentOrders", null));
                                    menuResults.add(generateByAttributes("5", "calendar",
                                            "预约租借管理", null, baseMenuResults));
                                }else{
                                    menuResults.get(menuResults.size()-1).getChildren().add(generateByAttributes("52", null, "待发货订单",
                                            "/tianyi/sentOrders", null));
                                }
                                break;
                            case '3':
                                if(!prevSameBaseflag) {
                                    baseMenuResults.add(generateByAttributes("53", null, "待催还订单",
                                            "/tianyi/remindingOrders", null));
                                    menuResults.add(generateByAttributes("5", "calendar",
                                            "预约租借管理", null, baseMenuResults));
                                }else{
                                    menuResults.get(menuResults.size()-1).getChildren().add(generateByAttributes("53", null, "待催还订单",
                                            "/tianyi/remindingOrders", null));
                                }
                                break;
                            case '4':
                                if(!prevSameBaseflag) {
                                    baseMenuResults.add(generateByAttributes("54", null, "已完成订单",
                                            "/tianyi/completedOrders", null));
                                    menuResults.add(generateByAttributes("5", "calendar",
                                            "预约租借管理", null, baseMenuResults));
                                }else{
                                    menuResults.get(menuResults.size()-1).getChildren().add(generateByAttributes("54", null, "已完成订单",
                                            "/tianyi/completedOrders", null));
                                }
                                break;
                        }
                    }
                    prevSameBaseflag = false;
                    break;

                case '6':
                    if(keyStr.length()==1) {
                        baseMenuResults.add(generateByAttributes("61", null, "回馈统计",
                                "/tianyi/returnAnalysis", null));
                        baseMenuResults.add(generateByAttributes("62", null, "KPI统计",
                                "/tianyi/kpiAnalysis", null));
                        menuResults.add(generateByAttributes("6", "table",
                                "数据分析", null, baseMenuResults));
                    }else if(keyStr.length()==2){
                        if((menuResults.size()>0)&&(menuResults.get(menuResults.size()-1).
                                getKey().charAt(0)=='6')){
                            prevSameBaseflag = true;
                        }
                        switch (keyStr.charAt(1)) {
                            case '1':
                                if(!prevSameBaseflag) {
                                    baseMenuResults.add(generateByAttributes("61", null, "回馈统计",
                                            "/tianyi/returnAnalysis", null));
                                    menuResults.add(generateByAttributes("6", "table",
                                            "数据分析", null, baseMenuResults));
                                }else{
                                    menuResults.get(menuResults.size()-1).getChildren().add(generateByAttributes("61", null, "回馈统计",
                                            "/tianyi/returnAnalysis", null));
                                }
                                break;
                            case '2':
                                if(!prevSameBaseflag) {
                                    baseMenuResults.add(generateByAttributes("62", null, "KPI统计",
                                            "/tianyi/kpiAnalysis", null));
                                    menuResults.add(generateByAttributes("6", "table",
                                            "数据分析", null, baseMenuResults));
                                }else{
                                    menuResults.get(menuResults.size()-1).getChildren().add(generateByAttributes("62", null, "KPI统计",
                                            "/tianyi/kpiAnalysis", null));
                                }
                                break;
                        }
                    }
                    prevSameBaseflag = false;
                    break;

                default:
            }
        }
        return menuResults;
    }

    private static BaseMenuResult generateByAttributes(String key, String icon, String title,
                                                       String url, List<BaseMenuResult> children){
        BaseMenuResult baseMenuResult = new BaseMenuResult();
        baseMenuResult.setKey(key);
        baseMenuResult.setIcon(icon);
        baseMenuResult.setTitle(title);
        baseMenuResult.setUrl(url);
        baseMenuResult.setChildren(children);
        return baseMenuResult;
    }
}
